package cn.hftoday.org;

import java.util.*;
import org.apache.log4j.*;
import cn.hftoday.db.*;
import cn.hftoday.core.*;
import cn.hftoday.util.*;

/**
 * person
 * @author lane.cn@gmail.com
 *
 */
public class Person extends PartyBase {
	private static final Logger LOG = Logger.getLogger(Person.class);
	
	public static final String ICON = "person_icon.png";
	
	private String code;
	private Calendar birthday = CalendarParser.INVALID_TIME;
	private Calendar enterTime = CalendarParser.INVALID_TIME;
	private String idCard;
	private String title;
	private String privatePhone;
	private String workPhone;
	private String address;
	private String companyId;
	
	public String getCode() {
		return code;
	}
	
	public void setCode(String code) {
		this.code = code;
	}
	
	public Calendar getBirthday() {
		return birthday;
	}
	
	public void setBirthday(Calendar birthday) {
		this.birthday = birthday;
	}
	
	public Calendar getEnterTime() {
		return enterTime;
	}
	
	public void setEnterTime(Calendar enterTime) {
		this.enterTime = enterTime;
	}
	
	public String getIdCard() {
		return idCard;
	}
	
	public void setIdCard(String idCard) {
		this.idCard = idCard;
	}
	
	public String getTitle() {
		return title;
	}
	
	public void setTitle(String title) {
		this.title = title;
	}
	
	public String getPrivatePhone() {
		return privatePhone;
	}
	
	public void setPrivatePhone(String privatePhone) {
		this.privatePhone = privatePhone;
	}
	
	public String getWorkPhone() {
		return workPhone;
	}
	
	public void setWorkPhone(String workPhone) {
		this.workPhone = workPhone;
	}
	
	public String getAddress() {
		return address;
	}
	
	public void setAddress(String address) {
		this.address = address;
	}
	
	public String getIcon() {
		return ICON;
	}
	
	public Account getAccount() {
		return (Account)Mapper.getInstance().queryForObject("SelectAccountByPerson", id);
	}
	
	public Company getCompany() {
		return (Company)Mapper.getInstance().queryForObject("SelectCompanyById", companyId);
	}
	
	public void setCompany(Company company) {
		if (company == null) {
			companyId = null;
		}
		else {
			companyId = company.getId();
		}
	}

	public String getPartyType() {
		return "person";
	}
	
	private static void _select(Object object, RecordSet rs, int i) {
		Person o = (Person)object;
		o.id = rs.getItem(i, "f_id");
		o.name = rs.getItem(i, "f_name");
		o.hidden = rs.getItem(i, "f_hidden").equals("1");
		o.createTime = CalendarParser.toCalendar(rs.getItem(i, "f_createtime"));
		o.updateTime = CalendarParser.toCalendar(rs.getItem(i, "f_updatetime"));
		o.code = rs.getItem(i, "f_code");
		o.birthday = CalendarParser.toCalendar(rs.getItem(i, "f_birthday"));
		o.enterTime = CalendarParser.toCalendar(rs.getItem(i, "f_entertime"));
		o.idCard = rs.getItem(i, "f_idcard");
		o.title = rs.getItem(i, "f_title");
		o.privatePhone = rs.getItem(i, "f_privatephone");
		o.workPhone = rs.getItem(i, "f_workphone");
		o.address = rs.getItem(i, "f_address");
		o.companyId = rs.getItem(i, "f_company");
	}
	
	private static String _insert(Object object, String sql) {
		Person o = (Person)object;
		return StringParser.formatSql(sql, 
			o.id,
			o.name,
			o.hidden ? "1" : "0",
			CalendarParser.toString(o.createTime),
			CalendarParser.toString(o.updateTime),
			o.code,
			CalendarParser.toString(o.birthday),
			CalendarParser.toString(o.enterTime),
			o.idCard,
			o.title,
			o.privatePhone,
			o.workPhone,
			o.address,
			o.companyId
		);
	}
	
	private static String _update(Object object, String sql) {
		Person o = (Person)object;
		return StringParser.formatSql(sql, 
			o.id,
			o.name,
			o.hidden ? "1" : "0",
			CalendarParser.toString(o.createTime),
			CalendarParser.toString(o.updateTime),
			o.code,
			CalendarParser.toString(o.birthday),
			CalendarParser.toString(o.enterTime),
			o.idCard,
			o.title,
			o.privatePhone,
			o.workPhone,
			o.address,
			o.companyId
		);
	}
	
	private static String _delete(Object object, String sql) {
		Person o = (Person)object;
		return StringParser.formatSql(sql, o.id);
	}
}
